package com.common.domainService;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.TownForm;
import com.common.DAO.ITownDAO;
import com.common.entity.Town;
import com.common.transformer.TownTransformer;

public class TownService implements ITownService {
	
	Logger logger = Logger.getLogger(TownService.class);
	/** IOC **/
	private ITownDAO townDAO;
	
	
	
	
	public List<TownForm> getAllTown(){
		logger.info("getAllTown()");
		List<TownForm> listTownForm = new ArrayList<TownForm>();
		List<Town> listTown= new ArrayList<Town>();
		try{
			listTown=this.getTownDAO().getAllTown();
			System.out.println("DEBUG size in DS:"+listTown.size());
		}catch (Exception e) {
			logger.error("Erreur dans TOWN DS to get All town", e);
		}
		
		for(Town current : listTown){
			listTownForm.add(TownTransformer.Town_TO_TownForm(current));
		}
		System.out.println("DEBUG listTownForm"+listTownForm.size());
		logger.info("getAllTown() done");
		return listTownForm;
	}
	
	
	public List<TownForm>  getTownByCp(String cp){
		logger.info("etTownByCp...");
		List<TownForm> listTownForm = new ArrayList<TownForm>();
		List<Town> listTown= new ArrayList<Town>();
		 listTown=this.getTownDAO().listTownByCp(cp);
		 //transformation
		 for(Town current : listTown){
			 listTownForm.add(TownTransformer.Town_TO_TownForm(current));
		 }
		logger.info("getTownByCp...");
		return listTownForm;
	}
	
	public TownForm getTownById(int id){
		logger.info("etTownByCp...");
		TownForm myTown=new TownForm();
		Town town=new Town();
		try{
			town=this.getTownDAO().getTownByid(id);
			myTown=TownTransformer.Town_TO_TownForm(town);
		}catch (Exception e) {
			logger.error("erreur getTown By Id in DS", e);
		}
		logger.info("etTownByCp done");
		return myTown;
	}
	
	
	
	
	/**************************************************************
	 * GETTER AND DAO                                             *
	 **************************************************************/
	/**
	 * @return the townDAO
	 */
	public ITownDAO getTownDAO() {
		return townDAO;
	}
	/**
	 * @param townDAO the townDAO to set
	 */
	public void setTownDAO(ITownDAO townDAO) {
		this.townDAO = townDAO;
	}
	

}
